package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleUser;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.users.UserBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

@ManagedBean(name="usersListBean")
@ViewScoped
public class UserListBean {

	@ManagedProperty(value="#{systemService}")
	private SystemService service;
	
	public SystemService getService() {
		return service;
	}

	public void setService(SystemService service) {
		this.service = service;
	}

	public List<UserBean> getSysUsers(){
		List<SimpleUser> users = service.getSysUsers();
		return UserBean.wrap(users,service);
	}
	
	public void newUser(){
		SimpleUser user = new SimpleUser();
		UserBean newUser = new UserBean(user, service);
		user.setDevices(new ArrayList<Device>());
		user.setUserName("Nuevo Usuario" + user.hashCode());
		user.setPassword("pass");
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selectedUser", newUser);
	}
	
}
